home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 1843 / 1843.xpi / content / firebug / sourceCache.js < prev    next >
Text File  |  2010-01-15  |  8KB  |  291 lines

  1. /* See license.txt for terms of usage */
  2.  
  3. FBL.ns(function() { with (FBL) {
  4.  
  5. // ************************************************************************************************
  6. // Constants
  7.  
  8. const Cc = Components.classes;
  9. const Ci = Components.interfaces;
  10. const nsIIOService = Ci.nsIIOService;
  11. const nsIRequest = Ci.nsIRequest;
  12. const nsICachingChannel = Ci.nsICachingChannel;
  13. const nsIScriptableInputStream = Ci.nsIScriptableInputStream;
  14. const nsIUploadChannel = Ci.nsIUploadChannel;
  15. const nsIHttpChannel = Ci.nsIHttpChannel;
  16.  
  17. const IOService = Cc["@mozilla.org/network/io-service;1"];
  18. const ioService = IOService.getService(nsIIOService);
  19. const ScriptableInputStream = Cc["@mozilla.org/scriptableinputstream;1"];
  20. const chromeReg = CCSV("@mozilla.org/chrome/chrome-registry;1", "nsIToolkitChromeRegistry");
  21.  
  22. const LOAD_FROM_CACHE = nsIRequest.LOAD_FROM_CACHE;
  23. const LOAD_BYPASS_LOCAL_CACHE_IF_BUSY = nsICachingChannel.LOAD_BYPASS_LOCAL_CACHE_IF_BUSY;
  24.  
  25. const NS_BINDING_ABORTED = 0x804b0002;
  26.  
  27. // ************************************************************************************************
  28.  
  29. Firebug.SourceCache = function(context)
  30. {
  31.     this.context = context;
  32.     this.cache = {};
  33. };
  34.  
  35. Firebug.SourceCache.prototype = extend(new Firebug.Listener(),
  36. {
  37.     isCached: function(url)
  38.     {
  39.         return (this.cache[url] ? true : false);
  40.     },
  41.  
  42.     loadText: function(url, method, file)
  43.     {
  44.         var lines = this.load(url, method, file);
  45.         return lines ? lines.join("") : null;
  46.     },
  47.  
  48.     load: function(url, method, file)
  49.     {
  50.         var response = this.cache[this.removeAnchor(url)];
  51.         if (response)
  52.             return response;
  53.  
  54.         var d = FBL.splitDataURL(url);  //TODO the RE should not have baseLine
  55.         if (d)
  56.         {
  57.             var src = d.encodedContent;
  58.             var data = decodeURIComponent(src);
  59.             var lines = splitLines(data)
  60.             this.cache[url] = lines;
  61.  
  62.             return lines;
  63.         }
  64.  
  65.         var j = FBL.reJavascript.exec(url);
  66.         if (j)
  67.         {
  68.             var src = url.substring(FBL.reJavascript.lastIndex);
  69.             var lines = splitLines(src);
  70.             this.cache[url] = lines;
  71.  
  72.             return lines;
  73.         }
  74.  
  75.         var c = FBL.reChrome.test(url);
  76.         if (c)
  77.         {
  78.             if (Firebug.filterSystemURLs)
  79.                 return ["Filtered chrome url "+url];  // ignore chrome
  80.  
  81.             var chromeURI = makeURI(url);
  82.             var localURI = chromeReg.convertChromeURL(chromeURI);
  83.             return this.loadFromLocal(localURI.spec);
  84.         }
  85.  
  86.         c = FBL.reFile.test(url);
  87.         if (c)
  88.         {
  89.             return this.loadFromLocal(url);
  90.         }
  91.  
  92.         // Unfortunately, the URL isn't available so, let's try to use FF cache.
  93.         // Notice that additional network request to the server can be made in
  94.         // this method (double-load).
  95.         return this.loadFromCache(url, method, file);
  96.     },
  97.  
  98.     store: function(url, text)
  99.     {
  100.         var tempURL = this.removeAnchor(url);
  101.  
  102.         var lines = splitLines(text);
  103.         return this.storeSplitLines(tempURL, lines);
  104.     },
  105.  
  106.     removeAnchor: function(url)
  107.     {
  108.         var index = url.indexOf("#");
  109.         if (index < 0)
  110.             return url;
  111.  
  112.         return url.substr(0, index);
  113.     },
  114.  
  115.     loadFromLocal: function(url)
  116.     {
  117.         // if we get this far then we have either a file: or chrome: url converted to file:
  118.         var src = getResource(url);
  119.         if (src)
  120.         {
  121.             var lines = splitLines(src);
  122.             this.cache[url] = lines;
  123.  
  124.             return lines;
  125.         }
  126.     },
  127.  
  128.     loadFromCache: function(url, method, file)
  129.     {
  130.         var doc = this.context.window.document;
  131.         if (doc)
  132.             var charset = doc.characterSet;
  133.         else
  134.             var charset = "UTF-8";
  135.  
  136.         var channel;
  137.         try
  138.         {
  139.             channel = ioService.newChannel(url, null, null);
  140.             channel.loadFlags |= LOAD_FROM_CACHE | LOAD_BYPASS_LOCAL_CACHE_IF_BUSY;
  141.  
  142.             if (method && (channel instanceof nsIHttpChannel))
  143.             {
  144.                 var httpChannel = QI(channel, nsIHttpChannel);
  145.                 httpChannel.requestMethod = method;
  146.             }
  147.         }
  148.         catch (exc)
  149.         {
  150.             return;
  151.         }
  152.  
  153.         if (url == this.context.browser.contentWindow.location.href)
  154.         {
  155.             if (channel instanceof nsIUploadChannel)
  156.             {
  157.                 var postData = getPostStream(this.context);
  158.                 if (postData)
  159.                 {
  160.                     var uploadChannel = QI(channel, nsIUploadChannel);
  161.                     uploadChannel.setUploadStream(postData, "", -1);
  162.                 }
  163.             }
  164.  
  165.             if (channel instanceof nsICachingChannel)
  166.             {
  167.                 var cacheChannel = QI(channel, nsICachingChannel);
  168.                 cacheChannel.cacheKey = getCacheKey(this.context);
  169.             }
  170.         }
  171.         else if ((method == "PUT" || method == "POST") && file)
  172.         {
  173.             if (channel instanceof nsIUploadChannel)
  174.             {
  175.                 // In case of PUT and POST, don't forget to use the original body.
  176.                 var postData = getPostText(file, this.context);
  177.                 if (postData)
  178.                 {
  179.                     var postDataStream = getInputStreamFromString(postData);
  180.                     var uploadChannel = QI(channel, nsIUploadChannel);
  181.                     uploadChannel.setUploadStream(postDataStream, "application/x-www-form-urlencoded", -1);
  182.                 }
  183.             }
  184.         }
  185.  
  186.         var stream;
  187.         try
  188.         {
  189.             stream = channel.open();
  190.         }
  191.         catch (exc)
  192.         {
  193.             return ["sourceCache.load FAILS for url="+url, exc.toString()];
  194.         }
  195.  
  196.         try
  197.         {
  198.             var data = readFromStream(stream, charset);
  199.             var lines = splitLines(data);
  200.             this.cache[url] = lines;
  201.             return lines;
  202.         }
  203.         catch (exc)
  204.         {
  205.             return ["sourceCache.load FAILS for url="+url, exc.toString()];
  206.         }
  207.         finally
  208.         {
  209.             stream.close();
  210.         }
  211.     },
  212.  
  213.     storeSplitLines: function(url, lines)
  214.     {
  215.         return this.cache[url] = lines;
  216.     },
  217.  
  218.     invalidate: function(url)
  219.     {
  220.         delete this.cache[url];
  221.     },
  222.  
  223.     getLine: function(url, lineNo)
  224.     {
  225.         var lines = this.load(url);
  226.         if (lines)
  227.         {
  228.             if (lineNo <= lines.length)
  229.                 return lines[lineNo-1];
  230.             else
  231.                 return (lines.length == 1) ? lines[0] : "("+lineNo+" out of range "+lines.length+")";
  232.         }
  233.         else
  234.             return "(no source for "+url+")";
  235.     }
  236. });
  237.  
  238. // xxxHonza getPostText and readPostTextFromRequest are copied from
  239. // net.js. These functions should be removed when this cache is
  240. // refactored due to the double-load problem.
  241. function getPostText(file, context)
  242. {
  243.     if (!file.postText)
  244.         file.postText = readPostTextFromPage(file.href, context);
  245.  
  246.     if (!file.postText)
  247.         file.postText = readPostTextFromRequest(file.request, context);
  248.  
  249.     return file.postText;
  250. }
  251.  
  252. // ************************************************************************************************
  253.  
  254. function getPostStream(context)
  255. {
  256.     try
  257.     {
  258.         var webNav = context.browser.webNavigation;
  259.         var descriptor = QI(webNav, Ci.nsIWebPageDescriptor).currentDescriptor;
  260.         var entry = QI(descriptor, Ci.nsISHEntry);
  261.  
  262.         if (entry.postData)
  263.         {
  264.             // Seek to the beginning, or it will probably start reading at the end
  265.             var postStream = QI(entry.postData, Ci.nsISeekableStream);
  266.             postStream.seek(0, 0);
  267.             return postStream;
  268.         }
  269.      }
  270.      catch (exc)
  271.      {
  272.      }
  273. }
  274.  
  275. function getCacheKey(context)
  276. {
  277.     try
  278.     {
  279.         var webNav = context.browser.webNavigation;
  280.         var descriptor = QI(webNav, Ci.nsIWebPageDescriptor).currentDescriptor;
  281.         var entry = QI(descriptor, Ci.nsISHEntry);
  282.         return entry.cacheKey;
  283.      }
  284.      catch (exc)
  285.      {
  286.      }
  287. }
  288.  
  289. // ************************************************************************************************
  290. }});
  291.